package com.hk.service.quartz;

import com.hk.commons.ConstantProduct;
import com.hk.commons.HqlFilter;
import com.hk.commons.entity.product.ProductInfo;
import com.hk.service.product.IProductInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.sql.Timestamp;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ProductStartQuartz extends BaseQuartz {
    private Logger logger = LoggerFactory.getLogger(ProductStartQuartz.class);

    @Autowired
    private IProductInfo prodService;

    private void startSale() {
        HqlFilter hqlFilter = new HqlFilter();

        Long curTimeLong = System.currentTimeMillis();
        Timestamp curTime = new Timestamp(curTimeLong);
        //获取未上架产品
        hqlFilter.addSql(" where startSaleTime is not null  and endSaleTime is not null " +
                " and startSaleTime < '" + curTime + "' and  endSaleTime > '" + curTime + "' " +
                " and  proStatus = " + ConstantProduct.PRODUCT_RELEASE_STATE_FALSE);


        List<ProductInfo> prodList = prodService.findByFilter(hqlFilter);

        if (prodList != null && prodList.size() > 0) {
            logger.info("startSale sql ={}", hqlFilter.getWhereAndOrderHql());
            logger.info("list size = {}" + prodList.size());
        }

        for (ProductInfo prod : prodList) {
            prod.setProStatus(ConstantProduct.PRODUCT_RELEASE_STATE_TRUE);
            prodService.update(prod);
            logger.info("prod will start sale name={}", prod.getName());
        }
    }

    private void endSale() {
        HqlFilter hqlFilter = new HqlFilter();

        Long curTimeLong = System.currentTimeMillis();
        Timestamp curTime = new Timestamp(curTimeLong);

        //获取未上架产品
        hqlFilter.addSql(" where  startSaleTime is not null  and endSaleTime is not null " +
                " and endSaleTime < '" + curTime + "' and proStatus = " + ConstantProduct.PRODUCT_RELEASE_STATE_TRUE);

        List<ProductInfo> prodList = prodService.findByFilter(hqlFilter);

        if (prodList != null && prodList.size() > 0) {
            logger.info("endSale use sql ={}", hqlFilter.getWhereAndOrderHql());

            logger.info("list size = {}" + prodList.size());
        }

        for (ProductInfo prod : prodList) {
            logger.info("prod will update to Sale Out sale name={}", prod.getName());
            prod.setProStatus(ConstantProduct.PRODUCT_RELEASE_STATE_OFF);
            prodService.update(prod);
            logger.info("prod will update to Sale Out sale name={}", prod.getName());
        }
    }

    private void saleOut() {
        HqlFilter hqlFilter = new HqlFilter();

        Long curTimeLong = System.currentTimeMillis();
        Timestamp curTime = new Timestamp(curTimeLong);

        //获取未设置售罄的产品
        hqlFilter.addSql(" where startSaleTime is not null  and endSaleTime is not null  and saleOutTime is not null " +
                " and saleOutTime < '" + curTime + "' and saleOut = " + ConstantProduct.PRO_NO_SALE_OUT);

        List<ProductInfo> prodList = prodService.findByFilter(hqlFilter);
        if (prodList != null && prodList.size() > 0) {
            logger.info("saleOut sql ={}", hqlFilter.getWhereAndOrderHql());
            logger.info("list size = {}" + prodList.size());
        }

        for (ProductInfo prod : prodList) {
            prod.setSaleOut(ConstantProduct.PRO_SALE_OUT);
            prodService.update(prod);
            logger.info("prod will update to Sale Out sale name={}", prod.getName());
        }
    }

    private void chgSaleTime() {
        HqlFilter hqlFilter = new HqlFilter();

        Long curTimeLong = System.currentTimeMillis();

        Timestamp curTime = new Timestamp(curTimeLong);

        //获取未设置售罄的产品
        hqlFilter.addSql(" where waitSale = 1 and  chgBuyTypeTime < '" + curTime + "' ");


        List<ProductInfo> prodList = prodService.findByFilter(hqlFilter);

        if (prodList != null && prodList.size() > 0) {
            logger.info("chgSaleTime sql ={}", hqlFilter.getWhereAndOrderHql());
            logger.info("list size = {}" + prodList.size());
        }

        for (ProductInfo prod : prodList) {
            prod.setWaitSale(ConstantProduct.PRODUCT_WAITSALE_FALSE);
            prod.setHotSale(ConstantProduct.PRODUCT_HOT_SELLING_TRUE);
            prodService.update(prod);
            logger.info("prod  will change waitSale false, hotSale true, chgSaleTime prodName={}", prod.getName());
        }
    }

    /**
     * 根据待售时间修改产品是否待售的状态
     */
    private void chgBuyTypeTime(){
        HqlFilter hqlFilter = new HqlFilter();
        Long curTimeLong = System.currentTimeMillis();
        Timestamp curTime = new Timestamp(curTimeLong);

        //获取未下架或未上架的产品
        StringBuilder sb = new StringBuilder();
        sb.append(" where endSaleTime is not null and chgBuyTypeTime is not null and  endSaleTime > '");
        sb.append(curTime);
        sb.append("'");
        hqlFilter.addSql(sb.toString());
        List<ProductInfo> prodList = prodService.findByFilter(hqlFilter);

        if (prodList != null && prodList.size() > 0) {
            logger.info("chgBuyTypeTime sql ={}", hqlFilter.getWhereAndOrderHql());
            logger.info("list size = {}" + prodList.size());
        }

        for (ProductInfo prod : prodList) {
            if(curTimeLong < prod.getChgBuyTypeTime().getTime()){
                prod.setWaitSale(1);
            }else{
                prod.setWaitSale(0);
            }
            prodService.update(prod);
            logger.info("update prod waitSale's status  name={}", prod.getName());
        }

    }


    @Override
    public void start() {
        ExecutorService executor = Executors.newFixedThreadPool(1);
        Runnable task = () -> {
            logger.info(" start to check prod sale status and saleOut status ");
            startSale();
            endSale();
            saleOut();
            chgSaleTime();
            chgBuyTypeTime();
        };
        executor.execute(task);
        executor.shutdown();
    }

}
